//
//  StarViewController.m
//  Mango
//
//  Created by lanou3g on 16/5/14.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "StarCollectionViewController.h"
#import "StarCollectionViewCell.h"
#import "DiscoverStarsModel.h"
#import "StarInfoViewController.h"
#import "SearchTableViewController.h"
#define AllStars_URL @"http://api.app.happyjuzi.com/foundpage/list/starlist"

@interface StarCollectionViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UISearchBarDelegate>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (strong, nonatomic) NSMutableArray   *dataSource_stars;
@property (assign, nonatomic) NSUInteger page;
@property (strong, nonatomic) UISearchBar *customSearchBar;
@property (strong, nonatomic) UIView *searchView;
@property (nonatomic, copy) NSString *currentSkinModel;//夜间模式
@end

@implementation StarCollectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"明星库";
    
    self.dataSource_stars = [NSMutableArray new];
    [self createCollectionView];
    
    [self createSearchBar];
    
    [self refreshData];
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:252/255.0 green:65/255.0 blue:19/255.0 alpha:1.0];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateSkinModel) name:SkinModelDidChangedNotification object:nil];
    [self updateSkinModel];

}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark --private Method--更新皮肤模式 接到模式切换的通知后会调用此方法
-(void)updateSkinModel {
    self.currentSkinModel = [[NSUserDefaults standardUserDefaults] stringForKey:CurrentSkinModelKey];
    if ([self.currentSkinModel isEqualToString:NightSkinModelValue]) {//夜间模式
        self.collectionView.backgroundColor = [UIColor colorWithRed:42/255.0 green:39/255.0 blue:43/255.0 alpha:1.0];
        self.searchView.backgroundColor = [UIColor colorWithRed:42/255.0 green:39/255.0 blue:43/255.0 alpha:1.0];
    } else {//日间模式

    }
    [self.collectionView reloadData];
}


- (void)refreshData {
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    self.collectionView.mj_header.automaticallyChangeAlpha = YES;
    [self.collectionView.mj_header beginRefreshing];
    
    self.collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
}

- (void)loadNewData {
    self.page = 1;
    [self loadDataFromServerWithPage:self.page];
}

- (void)loadMoreData {
    self.page++;
    [self loadDataFromServerWithPage:self.page];
}

- (void)loadDataFromServerWithPage:(NSUInteger)page {
    NSDictionary *paramDict = @{@"res":@"1152x1920",@"accesstoken":@"55ce1e68910a13aa8090590d5bb2a0de",@"pf":@"android",@"uid":@"3971130771637759",@"page":@(page),@"ts":@0,@"channel":@"yingyongbao",@"net":@"wifi",@"size":@"30",@"mac":@"38-bc-1a-25-a0-0f",@"ver":@"2.11"};
    [[AFHTTPSessionManager manager]GET:AllStars_URL parameters:paramDict progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dataDict = responseObject[@"data"];
        //明星库
        NSArray *stars = dataDict[@"list"];
        for (NSDictionary *starDict in stars) {
            DiscoverStarsModel *model = [DiscoverStarsModel new];
            [model setValuesForKeysWithDictionary:starDict];
            [self.dataSource_stars addObject:model];
        }
        [self.collectionView reloadData];
        [self.collectionView.mj_header endRefreshing];
        [self.collectionView.mj_footer endRefreshing];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self.collectionView.mj_header endRefreshing];
        [self.collectionView.mj_footer endRefreshing];
    }];
}

- (void)createCollectionView {
    // 设置flowLayout
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(100, 150);
    flowLayout.sectionInset = UIEdgeInsetsMake(50, 10, 0, 10);

    // 初始化一个UICollectionView对象
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:[UIScreen mainScreen].bounds collectionViewLayout:flowLayout];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView = collectionView;
    [self.collectionView registerNib:[UINib nibWithNibName:@"StarCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"cStar"];

    [self.view addSubview:collectionView];
}

- (void)createSearchBar {
    UISearchBar *searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 10, ScreenSizeW , 30)];
    searchBar.placeholder = @"搜索你感兴趣的明星";
    searchBar.delegate = self;
    searchBar.searchBarStyle = UISearchBarStyleMinimal;
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 64, ScreenSizeW, 50)];
    view.backgroundColor = [UIColor whiteColor];
    [view addSubview:searchBar];
    self.searchView = view;
    [self.view addSubview:view];
}

// 搜索框
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
    SearchTableViewController *searchVC = [[SearchTableViewController alloc]init];
    [self.navigationController pushViewController:searchVC animated:YES];
    return NO;
}

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of items
    return self.dataSource_stars.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    StarCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cStar" forIndexPath:indexPath];
    cell.model = self.dataSource_stars[indexPath.row];
#pragma mark --private Method--更新皮肤模式
    if ([self.currentSkinModel isEqualToString:NightSkinModelValue]) {//日间模式
        [cell updateToNightSkinMode];
    }else {
        [cell updateToDaySkinMode];
    }
    return cell;
}



- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    StarInfoViewController *starInfoVC = [[StarInfoViewController alloc]init];
    DiscoverStarsModel *model = self.dataSource_stars[indexPath.row];
    starInfoVC.sid = model.ID;
    [self.navigationController pushViewController:starInfoVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    if (self.view.window == nil && [self isViewLoaded]) {
        self.view = nil;
    }
}
@end
